package com.hpin.fansea.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseFragment;
import com.hpin.fansea.my.AboutActivity;
import com.hpin.fansea.my.AuthActivity;
import com.hpin.fansea.my.MessageActivity;
import com.hpin.fansea.my.MyInfoActivity;
import com.hpin.fansea.my.MyOrderActivity;
import com.hpin.fansea.my.MyRepairsActivity;
import com.hpin.fansea.my.MyServiceActivity;
import com.hpin.fansea.my.PayRecordActivity;
import com.hpin.fansea.ui.LoginActivity;
import com.hpin.fansea.ui.RegistActivity;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.umeng.analytics.MobclickAgent;


/**
 * 我的fragment
 * Created by Aaron on 2015/7/15.
 */
public class MyFragement extends BaseFragment implements View.OnClickListener {

    private Intent mIntent;
    private TextView tv_name;
    private CircleImageView cv_headpic;
    public DisplayImageOptions options;
    private TextView tv_user_name;
    private Button btn_login;
    private Button btn_register;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View myView = inflater.inflate(R.layout.fragment_my, null);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_headpic_default)
                .showImageForEmptyUri(R.mipmap.icon_headpic_default)
                .showImageOnFail(R.mipmap.icon_headpic_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

        initWidget(myView);

        return myView;
    }

    @Override
    public void onResume() {
        super.onResume();

        //头像
        ImageLoader.getInstance().displayImage(sp.getString(Constant.headPic, ""), cv_headpic, options);

        if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
            //游客登录
            btn_login.setVisibility(View.VISIBLE);
            btn_register.setVisibility(View.VISIBLE);

            tv_user_name.setVisibility(View.INVISIBLE);

        }else { //正常用户登录

            btn_login.setVisibility(View.INVISIBLE);
            btn_register.setVisibility(View.INVISIBLE);

            tv_user_name.setVisibility(View.VISIBLE);

            tv_user_name.setText(sp.getString(Constant.nickname, ""));
        }


        MobclickAgent.onPageStart(getClass().getName()); //统计页面
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }

    private void initWidget(View view) {

        cv_headpic = (CircleImageView) view.findViewById(R.id.cv_headpic);

        RelativeLayout rl_my_message = (RelativeLayout) view.findViewById(R.id.rl_my_message);//报修记录
        RelativeLayout rl_my_auth = (RelativeLayout) view.findViewById(R.id.rl_my_auth);//缴费记录
        RelativeLayout rl_my_order = (RelativeLayout) view.findViewById(R.id.rl_my_order);//我的订单
        RelativeLayout rl_my_repair_record = (RelativeLayout) view.findViewById(R.id.rl_my_repair_record);//购物车
        RelativeLayout rl_my_pay_record = (RelativeLayout) view.findViewById(R.id.rl_my_pay_record);//购物车
        RelativeLayout rl_my_service = (RelativeLayout) view.findViewById(R.id.rl_my_service);//我的收藏
        RelativeLayout rl_my_news = (RelativeLayout) view.findViewById(R.id.rl_my_news);//退出账号
        RelativeLayout rl_my_update = (RelativeLayout) view.findViewById(R.id.rl_my_update);//退出账号
        RelativeLayout rl_my_about = (RelativeLayout) view.findViewById(R.id.rl_my_about);//退出账号
        RelativeLayout rl_my_exit = (RelativeLayout) view.findViewById(R.id.rl_my_exit);//退出账号

        btn_login = (Button) view.findViewById(R.id.btn_login);
        btn_register = (Button) view.findViewById(R.id.btn_register);

        tv_user_name = (TextView) view.findViewById(R.id.tv_user_name);

        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);

        rl_my_repair_record.setOnClickListener(this);
        rl_my_pay_record.setOnClickListener(this);
        rl_my_service.setOnClickListener(this);
        rl_my_news.setOnClickListener(this);
        rl_my_update.setOnClickListener(this);
        rl_my_about.setOnClickListener(this);
        rl_my_exit.setOnClickListener(this);
        rl_my_message.setOnClickListener(this);
        rl_my_auth.setOnClickListener(this);
        rl_my_order.setOnClickListener(this);

    }

    public PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.rl_my_message://我的信息
                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能查看哦");
                    return;
                }
                // 我的信息页
                mIntent = new Intent(getActivity(), MyInfoActivity.class);
                getActivity().startActivity(mIntent);
                break;
            case R.id.rl_my_auth://申请认证

                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能认证哦");
                    return;
                }
                mIntent = new Intent(getActivity(), AuthActivity.class);
                getActivity().startActivity(mIntent);

                break;
            case R.id.rl_my_order://我的订单

                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能查看哦");
                    return;
                }

                mIntent = new Intent(getActivity(), MyOrderActivity.class);
                startActivity(mIntent);
                break;

            case R.id.rl_my_repair_record://报修记录

                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能查看哦");
                    return;
                }

                mIntent = new Intent(getActivity(), MyRepairsActivity.class);
                startActivity(mIntent);

                break;
            case R.id.rl_my_pay_record://缴费记录

                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能查看哦");
                    return;
                }

                if (Constant.AUTH_PASSED.equals(sp.getString(Constant.status, "")) || Constant.AUTH_USER.equals(sp.getString(Constant.status, ""))) {

                    //认证用户
                    mIntent = new Intent(getActivity(), PayRecordActivity.class);
                    startActivity(mIntent);

                }else if(Constant.REGISTER_USER.equals(sp.getString(Constant.status, ""))) {
                    //注册用户
                    new AlertDialog.Builder(getActivity()).setMessage("亲,认证用户才能查看哦").setNegativeButton("取消", null)
                    .setPositiveButton("去认证", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            mIntent = new Intent(getActivity(), AuthActivity.class);
                            startActivity(mIntent);

                        }
                    }).show();
                }
//
                break;
            case R.id.rl_my_service://我的服务

                mIntent = new Intent(getActivity(), MyServiceActivity.class);
                startActivity(mIntent);

                break;
            case R.id.rl_my_news://我的消息

                // 游客权限控制
                if (Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    remindLogin("亲,登录后才能查看哦");
                    return;
                }

                mIntent = new Intent(getActivity(), MessageActivity.class);
                startActivity(mIntent);

                break;
              case R.id.rl_my_update://版本更新


                break;

            case R.id.rl_my_about://关于

                mIntent = new Intent(getActivity(), AboutActivity.class);
                startActivity(mIntent);

                break;

            case R.id.btn_login://登录

                mApplication.clearActivity();
                mIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(mIntent);

                break;
            case R.id.btn_register://注册

                mApplication.clearActivity();
                mIntent = new Intent(getActivity(), RegistActivity.class);
                startActivity(mIntent);

                break;

            case R.id.rl_my_exit://退出

                quit();
                break;

        }
    }

    //退出
    private void quit() {

        new AlertDialog.Builder(getActivity()).setTitle("提示").setMessage("确定要退出吗?").setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                SharedPreferences.Editor edit = sp.edit();

//                //删除用户的数据信息
//                edit.putString(Constant.token, "");
//                edit.putString(Constant.communityId, "");
//                edit.putString(Constant.userName, "");
//                edit.putString(Constant.userId, "");
//                edit.putString(Constant.nickname, "");
//                edit.putString(Constant.headPic, "");
//                edit.putString(Constant.address, "");
//                edit.putString(Constant.password, "");
//                edit.putString(Constant.status, "");
//                edit.putString(Constant.email, "");
//                edit.putString(Constant.mobile, "");
//                edit.putString(Constant.cityId, "");
//                edit.putString(Constant.projectName, "");
//                edit.putString(Constant.houseCode, "");
//                edit.putString(Constant.currentCommunityId, "");
//                edit.putString(Constant.houseId, "");
//                edit.putString(Constant.gender, "");
//                edit.putString(Constant.birthday, "");
//                edit.putString(Constant.age, "");
//                edit.putString(Constant.idNo , "");
//                edit.putString(Constant.cityName, "");
//                edit.putString(Constant.communityName, "");
//                edit.putString(Constant.buildingNo, "");
//                edit.putString(Constant.unitNo, "");
//                edit.putString(Constant.houseNo, "");

                edit.clear();
                edit.commit();

                dialog.dismiss();

                mApplication.exit();

            }
        }).show();

    }
}
